<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\Playlet;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Model\AppletCouponPromotionActivity;
use App\Common\Response\SystemCode;
use App\Service\playlet\AppletCouponPromotionActivityService;

#[AutoController]
class AppletCouponPromotionActivityController  extends BaseController
{

    #[Inject]
    private AppletCouponPromotionActivityService $appletCouponPromotionActivityService;
    /**
     * 营销活动列表
     */
    public function Index(RequestInterface $request)
    {

        $data = $this->appletCouponPromotionActivityService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 营销活动创建
     */
    public function Create(RequestInterface $request){

        $result = $this->appletCouponPromotionActivityService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }
    /**
     * 营销活动删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $appletCouponPromotionActivityInfo = AppletCouponPromotionActivity::query()->where('applet_coupon_promotion_activity.id', $id)
                ->select(['applet_coupon_promotion_activity.id','applet.app_id','activity_id','applet_coupon_promotion_activity.applet_id'])
                ->leftJoin('applet','applet.id','=','applet_coupon_promotion_activity.applet_id')
                ->first();
            if (empty($appletCouponPromotionActivityInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序券不存在');
            }
            $result = $this->appletCouponPromotionActivityService->delete($appletCouponPromotionActivityInfo);
            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);
            }
        }
    }
    /**
     * 营销活动更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $appletCouponPromotionActivityInfo = AppletCouponPromotionActivity::query()
                ->select(['id','applet_id','activity_name','activity_id','applet_coupon_id','action_trigger','receive_limit','high_value_content','jump_text','recent_bubble_text','short_title','valid_begin_time','valid_end_time','status'])
                ->where('id',$id)->first();
            if(empty($appletCouponPromotionActivityInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '营销活动不存在');
            }
            if($request->getMethod() == 'GET'){

                $appletCouponPromotionActivityInfo->valid_time = [$appletCouponPromotionActivityInfo->valid_begin_time,$appletCouponPromotionActivityInfo->valid_end_time];

                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$appletCouponPromotionActivityInfo);

            }else{
                $result = $this->appletCouponPromotionActivityService->createOrUpdate($request->post(),true,$appletCouponPromotionActivityInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }

            }
        }
    }
    /**
     * 营销活动修改状态
     */
    public function updateStatus(int $id,RequestInterface $request){


        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $appletCouponPromotionActivityInfo = AppletCouponPromotionActivity::query()->where('applet_coupon_promotion_activity.id', $id)
                ->select(['applet_coupon_promotion_activity.id','applet.app_id','activity_id','applet_coupon_promotion_activity.applet_id'])
                ->leftJoin('applet','applet.id','=','applet_coupon_promotion_activity.applet_id')
                ->first();
            if(empty($appletCouponPromotionActivityInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '营销活动不存在');
            }
            $result = $this->appletCouponPromotionActivityService->updateStatus($appletCouponPromotionActivityInfo,$request->post());

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
            }else{
                return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

            }
        }
    }
}